export default {
  getFundDetail(id) {
    if (!id) { return Promise.resolve().then(() => { }); }
    return getOwhatRanking(id).then(ranking => {
      return {
        raised: ranking.data.rankinglist.reduce((total, item) => total + parseFloat(item.amount), 0),
        backers: ranking.data.rankinglist.length,
      }
    }).catch(console.log)
  }
};


function getOwhatRanking(id) {
  return fetch("https://m.owhat.cn/api", {
    "headers": {
      "accept": "application/json",
      "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5,zh-TW;q=0.4",
      "cache-control": "no-cache",
      "content-type": "application/x-www-form-urlencoded",
      "pragma": "no-cache",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      "x-requested-with": "XMLHttpRequest"
    },
    "referrer": `https://m.owhat.cn/shop/toplist.html?id=${id}`,
    "referrerPolicy": "no-referrer-when-downgrade",
    "body": getBody(id),
    "method": "POST",
    "mode": "cors",
    "credentials": "include"
  }).then(
    res => res.json()
  );
}

function getBody(id) {
  let base = 'cmd_s=shop.goods&cmd_m=findrankingbygoodsid&v=1.5.6L'
  let client = encodeURIComponent(JSON.stringify({
    "platform": "mobile",
    "version": "5.5.0",
    "deviceid": "444c8302-9134-1396-b7e1-6359f024231a",
    "channel": "owhat"
  }));
  let data = encodeURIComponent(JSON.stringify({ "goodsid": `${id}`, "pagenum": 0, "pagesize": 20000 }));
  return `${base}&client=${client}&data=${data}`;
}
